package Views;

import Controllers.EmployeeCTR;
import Models.*;
import javax.swing.JOptionPane;

/**
 * @author Thomas, Erik og Morten
 */
public class EmployeeInfoView extends javax.swing.JFrame
{
	private Employee employee;
	private EmployeeCTR controller = new EmployeeCTR();

    public EmployeeInfoView(Employee employee)
	{
		this.employee = employee;
        initComponents();
		nameTF.setText(this.employee.getName());
		if( this.employee.getLocation() == Location.LumberYard)
		{ lumberYardRB.setSelected(true); }
		else
		{ hardwareStoreRB.setSelected(true);}
		if(!employee.isActivated())
		{
			activatedCB.setSelected(false);
		}
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        locationBG = new javax.swing.ButtonGroup();
        descriptionLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        locationLabel = new javax.swing.JLabel();
        lumberYardRB = new javax.swing.JRadioButton();
        hardwareStoreRB = new javax.swing.JRadioButton();
        editButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        activatedCB = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Information om ansatte");

        descriptionLabel.setText("Her ses information om den valgte ansatte");

        nameLabel.setText("Navn:");

        nameTF.setEnabled(false);

        locationLabel.setText("Afdeling:");

        locationBG.add(lumberYardRB);
        lumberYardRB.setText("Trælasthandel");
        lumberYardRB.setEnabled(false);

        locationBG.add(hardwareStoreRB);
        hardwareStoreRB.setText("Byggemarked");
        hardwareStoreRB.setEnabled(false);

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Slet");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        activatedCB.setSelected(true);
        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(descriptionLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 64, Short.MAX_VALUE)
                        .add(activatedCB))
                    .add(layout.createSequentialGroup()
                        .add(nameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nameTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(locationLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lumberYardRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(hardwareStoreRB))
                    .add(layout.createSequentialGroup()
                        .add(editButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 70, Short.MAX_VALUE)
                        .add(closeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(descriptionLabel)
                    .add(activatedCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(locationLabel)
                    .add(lumberYardRB)
                    .add(hardwareStoreRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editButton)
                    .add(removeButton)
                    .add(closeButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editButtonActionPerformed
	{//GEN-HEADEREND:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger"))
		{
			nameTF.setEnabled(true);
			nameTF.setEditable(true);
			lumberYardRB.setEnabled(true);
			hardwareStoreRB.setEnabled(true);
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		}
		else if (editButton.getText().equals("Gem"))
		{
			Location location = null;
			if(lumberYardRB.isSelected())
			{
				location = Location.LumberYard;
			}
			else
			{
				location = Location.HardwareStore;
			}
			this.employee.setName(nameTF.getText());
			this.employee.setLocation(location);
			nameTF.setEnabled(false);
			nameTF.setEditable(false);
			lumberYardRB.setEnabled(false);
			hardwareStoreRB.setEnabled(false);
			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
	}//GEN-LAST:event_editButtonActionPerformed

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeButtonActionPerformed
	{//GEN-HEADEREND:event_removeButtonActionPerformed
		int choice = JOptionPane.showConfirmDialog(this, "Er du sikker på at du vil slette "+employee.getName()+"?", "Bekræftelse af sletning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(choice == 0)
		{
			this.controller.removeEmployee(employee);
			this.dispose();
		}
	}//GEN-LAST:event_removeButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_closeButtonActionPerformed
	{//GEN-HEADEREND:event_closeButtonActionPerformed
		this.dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JRadioButton hardwareStoreRB;
    private javax.swing.ButtonGroup locationBG;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JRadioButton lumberYardRB;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables

}
